#####
# Replication for: "Can Political Speech Foster Tolerance of Immigrants?" by Schleiter, Tavits, and Ward.
# Table S.7
#####

library(here)
library(data.table)
library(texreg)

# source the custom functions
source("functions.R")

# load the pooled data if not already in workspace
if(!exists("pooled")){
  pooled <- fread(here("data", "pooled.csv"))
}

# define the imbalanced covaraites
imbMain <- c("age", "ageSq")

# Fitting -----------------------------------------------------------------

s7_1 <- noCov(immPCA, chatty = F)
s7_2 <- allCov(immPCA, chatty = F)
s7_3 <- flexCov(immPCA, covs = imbMain, chatty = F)

s7_4 <- noCov(imm_neighbors2, chatty = F)
s7_5 <- allCov(imm_neighbors2, chatty = F)
s7_6 <- flexCov(imm_neighbors2, covs = imbMain, chatty = F)

s7_7 <- noCov(imm_increase2, chatty = F)
s7_8 <- allCov(imm_increase2, chatty = F)
s7_9 <- flexCov(imm_increase2, covs = imbMain, chatty = F)

s7_10 <- noCov(immAVG, chatty = F)
s7_11 <- allCov(immAVG, chatty = F)
s7_12 <- flexCov(immAVG, covs = imbMain, chatty = F)

# Reporting ---------------------------------------------------------------

# in the document, these three outputs are merged to a single latex table.

texregPS(
  list(s7_1, s7_2, s7_3),
  custom.model.names = paste0("(", 1:3, ")"),
  custom.gof.rows = list(
    "All Covariates" = c("", "$\\checkmark$", ""),
    "Imbalanced Covaraites" = c("", "", "$\\checkmark$")
  )
)

texregPS(
  list(s7_4, s7_5, s7_6),
  custom.model.names = paste0("(", 4:6, ")"),
  custom.gof.rows = list(
    "All Covariates" = c("", "$\\checkmark$", ""),
    "Imbalanced Covaraites" = c("", "", "$\\checkmark$")
  )
)

texregPS(
  list(s7_7, s7_8, s7_9),
  custom.model.names = paste0("(", 7:9, ")"),
  custom.gof.rows = list(
    "All Covariates" = c("", "$\\checkmark$", ""),
    "Imbalanced Covaraites" = c("", "", "$\\checkmark$")
  )
)

texregPS(
  list(s7_10, s7_11, s7_12),
  custom.model.names = paste0("(", 10:12, ")"),
  custom.gof.rows = list(
    "All Covariates" = c("", "$\\checkmark$", ""),
    "Imbalanced Covaraites" = c("", "", "$\\checkmark$")
  )
)
